﻿/*
 * Released under Microsoft Public License (Ms-PL) 
 * Sponsored by Development Platform Evangelism unit of Microsoft Israel
 * 
 * Copyright © 2008 by Tamir Khason
 * http://blogs.microsoft.co.il/blogs/tamir/
 * http://sharpsoft.net/
 * 
 * More information including licensing and term of use
 * can be found on http://www.codeplex.com/SilverlightRTL/
 * 
 */

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace System.Windows.BidiControls
{
    /// <summary>
    /// Represents a control with single piece of content
    /// </summary>
    [ContentProperty("Content")]
    [TemplatePart(Name = ContentControl.ContentElementName, Type = typeof(System.Windows.Controls.ContentControl))]
    public class ContentControl : Control
    {
        #region props

        #region Content
        internal object LocalContent
        {
            get { return GetValue(LocalContentProperty); }
            set { SetValue(LocalContentProperty, value); }
        }
        static readonly DependencyProperty LocalContentProperty =
                    DependencyProperty.Register(
                          "LocalContent",
                          typeof(object),
                          typeof(ContentControl),
                          null);
        /// <summary> 
        /// Gets or sets the Content possible Value of the object object.
        /// </summary> 
        public object Content
        {
            get { return (object)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        /// <summary> 
        /// Identifies the Content dependency property.
        /// </summary> 
        public static readonly DependencyProperty ContentProperty =
                    DependencyProperty.Register(
                          "Content",
                          typeof(object),
                          typeof(ContentControl),
                          new PropertyMetadata(OnContentPropertyChanged));

        /// <summary>
        /// ContentProperty property changed handler. 
        /// </summary>
        /// <param name="d">ContentControl that changed its Content.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
        private static void OnContentPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ContentControl _ContentControl = d as ContentControl;
            if (_ContentControl != null)
            {
                _ContentControl.resetData(e.NewValue);
                _ContentControl.OnContentChanged(e.OldValue, e.NewValue);
            }
        }

        protected virtual void OnContentChanged(object oldContent, object newContent) { }

        void resetData(object data)
        {
            if (data is string)
                resetTextData((string)data);
        }
        void resetTextData(string data)
        {
            if (this.FlowDirection == FlowDirection.RightToLeft)
            {
                this.LocalContent = NBidi.NBidi.LogicalToVisual(data);
            }
            else
            {
                this.LocalContent = data;
            }
        }
        #endregion Content

        #region ContentTemplate

        /// <summary> 
        /// Gets or sets the ContentTemplate possible Value of the DataTemplate object.
        /// </summary> 
        public DataTemplate ContentTemplate
        {
            get { return (DataTemplate)GetValue(ContentTemplateProperty); }
            set { SetValue(ContentTemplateProperty, value); }
        }

        /// <summary> 
        /// Identifies the ContentTemplate dependency property.
        /// </summary> 
        public static readonly DependencyProperty ContentTemplateProperty =
                    DependencyProperty.Register(
                          "ContentTemplate",
                          typeof(DataTemplate),
                          typeof(ContentControl),
                          null);

        #endregion ContentTemplate

        #region IsEnabled

        /// <summary> 
        /// Gets or sets the IsEnabled possible Value of the bool object.
        /// </summary> 
        public bool IsEnabled
        {
            get { return (bool)GetValue(IsEnabledProperty); }
            set { SetValue(IsEnabledProperty, value); }
        }

        /// <summary> 
        /// Identifies the IsEnabled dependency property.
        /// </summary> 
        public static readonly DependencyProperty IsEnabledProperty =
                    DependencyProperty.Register(
                          "IsEnabled",
                          typeof(bool),
                          typeof(ContentControl),
                          new PropertyMetadata(OnIsEnabledChanged));

        /// <summary>
        /// IsEnabledProperty property changed handler. 
        /// </summary>
        /// <param name="d">ContentControl that changed its IsEnabled.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
        private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ContentControl _ContentControl = d as ContentControl;
            if (_ContentControl != null)
            {
                _ContentControl.OnIsEnabledChanged((bool)e.NewValue);
            }
        }

        protected virtual void OnIsEnabledChanged(bool isEnabled) { }


        #endregion IsEnabled

        #endregion

        #region overrides
        protected override void OnFlowDirectionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            base.OnFlowDirectionPropertyChanged(d, e);
            resetData(Content);
        }
        #endregion

        #region ctor
        public ContentControl()
        {
            DefaultStyleKey = typeof(System.Windows.BidiControls.ContentControl);
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ContentElement = GetTemplateChild(ContentElementName) as System.Windows.Controls.ContentControl;

            resetData(Content);
        }
        #endregion

        #region Template Parts
        internal System.Windows.Controls.ContentControl ContentElement { get; set; }
        internal const string ContentElementName = "ContentElement";
        #endregion
    }
}
